package com.example.userlogincenter.userCenter.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.userlogincenter.userCenter.entity.ApikeyInfo;
import com.example.userlogincenter.userCenter.mapper.ApikeyInfoMapper;
import com.example.userlogincenter.userCenter.service.ApikeyInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.UUID;

/**
* @author ASUS
* @description 针对表【apikey_info】的数据库操作Service实现
* @createDate 2023-04-01 02:18:44
*/
@Service
public class ApikeyInfoServiceImpl extends ServiceImpl<ApikeyInfoMapper, ApikeyInfo>
    implements ApikeyInfoService {


    @Override
    public boolean saveKey(ApikeyInfo apikeyInfo) {
        ApikeyInfo apikeyInfoSave = new ApikeyInfo();
        BeanUtils.copyProperties(apikeyInfo,apikeyInfoSave);
        if (apikeyInfoSave.getAkMemberid() != null || !apikeyInfoSave.getAkMemberid().equals("")){
            String key = UUID.randomUUID().toString();
            String secret = UUID.randomUUID().toString();
            apikeyInfoSave.setAkKey(key);
            apikeyInfoSave.setAkSecret(secret);
            if ( apikeyInfoSave.getAkEnddate() == null ){
                apikeyInfoSave.setAkForever(1);
            }
            int insert = this.baseMapper.insert(apikeyInfoSave);
            if (insert == 1){
                return true;
            }
            return false;
        }else{
            return false;
        }


    }

    @Override
    public ApikeyInfo lookupKey(String userid) {
        QueryWrapper<ApikeyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ak_memberid",userid);
        ApikeyInfo apikeyInfo = this.baseMapper.selectOne(queryWrapper);
        return apikeyInfo;
    }
}




